package com.aaa.base.modules.visitor.ocr;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.aliyun.ocr.Client;
import com.aliyun.ocr.models.*;
import com.aliyun.teautil.models.RuntimeOptions;
import com.aaa.base.modules.visitor.utils.PicUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.Map;


/**
 * @author Marder
 * @date 2020-09-28 15:31
 * 选择阿里云达摩院视觉智能开放平台java本地上传，否则要开通oss
 * 前端不用传原图，就传压缩的
 */
@Service("ocrService")
public class OcrServiceImpl implements OcrService {

    private Client ocrClient;
    private RuntimeOptions runtime;

    private Logger log = LoggerFactory.getLogger(OcrServiceImpl.class);

    /**
     * 项目启动时，初始化阿里云client
     *
     * @throws Exception
     */
    @PostConstruct
    private void init() throws Exception {
        Config config = new Config();
        config.type = OcrConstant.ALIYUN_TYPE;
        config.regionId = OcrConstant.ALIYUN_REGIONID;
        config.accessKeyId = OcrConstant.ALIYUN_ACCESSKEYID;
        config.accessKeySecret = OcrConstant.ALIYUN_ACCESSKEYSECRET;
        config.endpoint = OcrConstant.ALIYUN_ENDPOINT;
        //创建Client对象
        ocrClient = new Client(config);
        //创建RuntimeOptions对象
        runtime = new RuntimeOptions();
        log.info("阿里云ocr Client init success!");
    }


    @Override
    public Map<String, String> recognizeIdCard(String filePath, String side) {
        //身份证request
        RecognizeIdentityCardAdvanceRequest request = new RecognizeIdentityCardAdvanceRequest();

        //读取输入流
        InputStream inputStream = PicUtils.getInputStreamOfImageFromNetByUrl(filePath);
        request.imageURLObject = inputStream;

        // 身份证正反面
        request.side = side;
        //身份证
        RecognizeIdentityCardResponse response = null;
        try {
            response = ocrClient.recognizeIdentityCardAdvance(request, runtime);
        } catch (Exception e) {
            e.printStackTrace();
            //识别异常
            return null;
        }

        //身份证
        if (OcrConstant.IDCARD_FACE_SIDE.equals(side)) {
            return JSON.parseObject(JSON.toJSONString(response.data.frontResult), new TypeReference<Map<String, String>>() {
            });
        } else {
            return JSON.parseObject(JSON.toJSONString(response.data.backResult), new TypeReference<Map<String, String>>() {
            });
        }

    }

    @Override
    public Map<String, String> recognizeBankCard(String filePath) {
        //银行卡request
        RecognizeBankCardAdvanceRequest request = new RecognizeBankCardAdvanceRequest();
        //读取输入流
        InputStream inputStream = PicUtils.getInputStreamOfImageFromNetByUrl(filePath);
        request.imageURLObject = inputStream;
        //银行卡
        RecognizeBankCardResponse response = null;
        try {
            response = ocrClient.recognizeBankCardAdvance(request, runtime);
        } catch (Exception e) {
            e.printStackTrace();
            //识别异常
            return null;
        }

        return JSON.parseObject(JSON.toJSONString(response.data), new TypeReference<Map<String, String>>() {
        });

    }

    @Override
    public Map<String, String> recognizeLicensePlate(String filePath) {
        //车牌request
        RecognizeLicensePlateAdvanceRequest request = new RecognizeLicensePlateAdvanceRequest();

        Long inputS= System.currentTimeMillis();

        //读取输入流
        InputStream inputStream = PicUtils.getInputStreamOfImageFromNetByUrl(filePath);
        request.imageURLObject = inputStream;

        Long inputE= System.currentTimeMillis();

        log.info("读取耗时--"+(inputE-inputS));

        Long reS=System.currentTimeMillis();

        //车牌
        RecognizeLicensePlateResponse response = null;
        try {
            response = ocrClient.recognizeLicensePlateAdvance(request, runtime);
        } catch (Exception e) {
            e.printStackTrace();
            //识别异常
            return null;
        }

        Long reE= System.currentTimeMillis();

        log.info("阿里云识别耗时--"+(reE-reS));

        return JSON.parseObject(JSON.toJSONString(response.data.plates), new TypeReference<Map<String, String>>() {});

    }
}
